package com.ruoyi.file.service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.file.redis.RedisConstants;
import com.ruoyi.common.core.utils.HMACSHA256;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CheckSignService {

    @Autowired
    private RedisService redisService;

    public boolean checkSign(String appId, long ts, String sign) {
        if (sign == null) {
            return false;
        }
        String secret = Convert.toStr(redisService.getCacheMapValue(RedisConstants.getAppKey(appId), RedisConstants.APP_SECRET));
        if (secret == null) {
            return false;
        }
        return sign.equalsIgnoreCase(
                HMACSHA256.sign(String.format("%s,%d", appId, ts), secret));
    }

    public boolean checkSign(String appId, long ts, String fileId, Long expires, String sign) {
        if (sign == null) {
            return false;
        }
        String secret = Convert.toStr(redisService.getCacheMapValue(RedisConstants.getAppKey(appId), RedisConstants.APP_SECRET));
        if (secret == null) {
            return false;
        }
        return sign.equalsIgnoreCase(
                HMACSHA256.sign(String.format("%s,%d,%s,%d", appId, ts, fileId, expires), secret));
    }

    public String getAppTag(String appId) {
        return Convert.toStr(redisService.getCacheMapValue(RedisConstants.getAppKey(appId), RedisConstants.APP_TAG));
    }

    public String getFtpPath(String appId) {
        return Convert.toStr(redisService.getCacheMapValue(RedisConstants.getAppKey(appId), RedisConstants.APP_FTP));
    }

}
